<template> <!-- 领用申请-->
  <el-dialog
    :title="!dataForm.assetsId ? '领用申请' : '领用申请'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-row type="flex">
        <el-col :span="12">
            <el-form-item label="资产名称" prop="assetsName">
              <el-input v-model="dataForm.assetsName" placeholder="资产名称" disabled></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="资产编号" prop="assetsName">
              <el-input v-model="dataForm.assetsNum" placeholder="资产编号" disabled></el-input>
            </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="申请日期" prop="receiveTime">
        <!--      <el-input v-model="dataForm.assetsBuytime" placeholder="购入日期"></el-input>-->
        <el-date-picker v-model="dataForm.receiveTime" type="date" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="receiveDesc">
        <el-input v-model="dataForm.receiveDesc" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        receiveId: 0,    // 领用ID
        assetsName: 0,     // 资产名
        assetsNum: '',      // 资产编号
        userName: '',      // 使用人
        receiveTime: '',    //  领用时间
        createBy: '',       // 审核人
        createTime: '',     // 审核时间
        receiveDesc: ''     // 备注
      },
      dataRule: {
        assetsName: [
          { required: true, message: '资产名称不能为空', trigger: 'blur' }
        ],
        receiveTime: [
          { required: true, message: '申请日期不能为空', trigger: 'blur' }
        ],
        receiveDesc: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.assetsId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.assetsId) {
          this.$http({
            url: this.$http.adornUrl(`/assets/info/info/${this.dataForm.assetsId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.assetsName = data.info.assetsName
          /*    this.dataForm.assetsBuytime = data.info.assetsBuytime */
              this.dataForm.assetsNum = data.info.assetsNum  // 资产编号
              this.dataForm.assetsAdmin = data.info.assetsAdmin  // 领用人
              this.dataForm.assetsImg = data.info.assetsImg
              this.dataForm.assetsDesc = data.info.assetsDesc   // 描述
              this.dataForm.assetsStatus = data.info.assetsStatus   // 状态
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/assets/receive/applySave'),
            method: 'post',
            data: this.$http.adornData({
              'assetsName': this.dataForm.assetsName,
              'assetsNum': this.dataForm.assetsNum,   // 资产编号
              'userName': this.$store.state.user.name,
              'receiveTime': this.dataForm.receiveTime,
              'createBy': this.dataForm.createBy,
              'createTime': this.dataForm.createTime,
              'receiveDesc': this.dataForm.receiveDesc
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                  this.dataForm = {}// 申请领用后清空上条数据
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
